package cn.xfdzx.android.apps.shop.adapter;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.MainActivity;
import cn.xfdzx.android.apps.shop.bean.AddressListBeen;
import cn.xfdzx.android.apps.shop.evenbus.HomeAdressMessage;
import cn.xfdzx.android.apps.shop.util.Utils;

import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;

/**
 * Creator: zf.
 * Date: 2019/1/23.
 * Description:我的地址管理列表适配器.
 */
public class MyAddressListAdapter extends BaseQuickAdapter<AddressListBeen.DataBean, BaseViewHolder>  {
    private OnItemOnClickListener onItemOnClickListener;
    private List<AddressListBeen.DataBean> mList;
    public MyAddressListAdapter(@Nullable List<AddressListBeen.DataBean> data) {
        super(R.layout.item_firstaddress_list, data);
    }

    @Override
    public void setData(int index, @NonNull AddressListBeen.DataBean data) {
        super.setData(index, data);
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final AddressListBeen.DataBean date) {
        TextView tv_tag = viewHolder.getView(R.id.tv_tag);
        viewHolder.setText(R.id.tv_name,date.getRealname())
        .setText(R.id.tv_phone,date.getMobile())
        .setText(R.id.tv_address,date.getAddress_info()+date.getHouse_number());
        if (date.getLabel() == 1) {
            tv_tag.setText("家");
        } else if (date.getLabel() == 2) {
            tv_tag.setText("公司");
        } else if (date.getLabel() == 3) {
            tv_tag.setText("公司");
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()){
                EventBus.getDefault().post(new HomeAdressMessage(date.getAddress_info(),date.getLat(),date.getLng()));
                Intent intent = new Intent(mContext, MainActivity.class);
                intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(FLAG_ACTIVITY_SINGLE_TOP);
                mContext.startActivity(intent);
            }}
        });
    }
    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<AddressListBeen.DataBean> list){
        this.mList = list;
        super.replaceData(list);
    }

    public void setItemOnClickListener(OnItemOnClickListener listener){
        this.onItemOnClickListener = listener;
    }

    public interface OnItemOnClickListener {
        void onItemClick(int address_id, int type, AddressListBeen.DataBean date);
    }

}
